﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibrarySystem
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string _val = Request.QueryString["val"];
            string _term = Server.UrlDecode(Request.QueryString["term"]);
            _term = _term.Replace(" ", "%");

            if (_val != "")
            {
                if (_val == "bkTitle" || _val == "bkAuthor" || _val == "bkISBN")
                {
                    if (_val == "bkTitle")
                    {
                        Global.SearchResultList = BusinessLayer.Book.searchByISBNorTitle("Title", _term);
                    }
                    else if (_val == "bkAuthor")
                    {
                        Global.SearchResultList = BusinessLayer.Book.getByAuthorName(_term);
                    }
                    else if (_val == "bkISBN")
                    {
                        Global.SearchResultList = BusinessLayer.Book.searchByISBNorTitle("ISBN", _term);
                    }

                    Global.SearchResultList = Global.SearchResultList.OrderBy(o => o.Title).ToList();
                    Global.SearchResultList = Global.SearchResultList.GroupBy(o => o.Title).Select(o => o.First()).ToList();
                    bookSearchList.DataSource = Global.SearchResultList;
                    bookSearchList.DataBind();
                    searchResCount.Text = "Antal träffar: " + Global.SearchResultList.Count;
                    bookListPanel.Visible = true;
                    authorListPanel.Visible = false;
                    borrowerListPanel.Visible = false;
                }
                else if (_val == "authName")
                {
                    Global.SearchAuthorResultList = BusinessLayer.Author.getByName(_term);

                    Global.SearchAuthorResultList = Global.SearchAuthorResultList.OrderBy(o => o.FirstName).ToList();
                    authorSearchList.DataSource = Global.SearchAuthorResultList;
                    authorSearchList.DataBind();
                    searchResCount.Text = "Antal träffar: " + Global.SearchAuthorResultList.Count;
                    bookListPanel.Visible = false;
                    authorListPanel.Visible = true;
                    borrowerListPanel.Visible = false;
                }
                else if (_val == "brwrName")
                {
                    Global.SearchBorrowerResultList = BusinessLayer.Borrower.getBorrowerByName(_term);
                    Global.SearchBorrowerResultList = Global.SearchBorrowerResultList.OrderBy(o => o.Name).ToList();
                    borrowerSearchList.DataSource = Global.SearchBorrowerResultList;
                    borrowerSearchList.DataBind();
                    searchResCount.Text = "Antal träffar: " + Global.SearchBorrowerResultList.Count;
                    bookListPanel.Visible = false;
                    authorListPanel.Visible = false;
                    borrowerListPanel.Visible = true;
                }
                                
            }
        }

        protected void bookSearchList_DataBound(object sender, EventArgs e)
        {
            DataPager1.Visible = (DataPager1.PageSize < DataPager1.TotalRowCount);
            DataPager2.Visible = (DataPager2.PageSize < DataPager2.TotalRowCount);
        }

        protected void authorSearchList_DataBound(object sender, EventArgs e)
        {
            DataPager3.Visible = (DataPager3.PageSize < DataPager3.TotalRowCount);
            DataPager4.Visible = (DataPager4.PageSize < DataPager4.TotalRowCount);
        }

        protected void borrowerSearchList_DataBound(object sender, EventArgs e)
        {
            DataPager5.Visible = (DataPager5.PageSize < DataPager5.TotalRowCount);
            DataPager6.Visible = (DataPager6.PageSize < DataPager6.TotalRowCount);
        }

        protected void bookDataPager_PreRender(object sender, EventArgs e)
        {
            bookSearchList.DataSource = Global.SearchResultList;
            bookSearchList.DataBind();
        }

        protected void authorDataPager_PreRender(object sender, EventArgs e)
        {
            authorSearchList.DataSource = Global.SearchAuthorResultList;
            authorSearchList.DataBind();
        }

        protected void borrowerDataPager_PreRender(object sender, EventArgs e)
        {
            borrowerSearchList.DataSource = Global.SearchBorrowerResultList;
            borrowerSearchList.DataBind();
        }

        protected void booklistItem_Clicked(object sender, CommandEventArgs e)
        {
            string itemISBN = e.CommandName.ToString();
            Response.Redirect("adminBookDetail.aspx?isbn=" + itemISBN);
        }

        protected void authorlistItem_Clicked(object sender, CommandEventArgs e)
        {
            string itemAid = e.CommandName.ToString();
            Response.Redirect("adminAuthorDetail.aspx?aid=" + itemAid);
        }

        protected void borrowerlistItem_Clicked(object sender, CommandEventArgs e)
        {
            string itemID = e.CommandName.ToString();
            Response.Redirect("adminBorrowerDetail.aspx?persID=" + itemID);
        }
    }
}